from base.web import BaseHandler, SHandler
import uuid
import os
import hashlib
import json
class login(BaseHandler):
    def post(self):
        h = hashlib.sha256()
        h.update(bytes(self.get_argument('password', ''), encoding='utf8'))
        query_params = dict(
            username = '\'' + self.get_argument('username', '') + '\'',
            password = '\'' + h.hexdigest() + '\''
        )
        try:
            user = self.query_sql('''select adaccount from pj_config where adaccount={username} and adpwd={password};'''.format(**query_params))[0]
            if user['adaccount']:
                self.set_secure_cookie('sessionid', user['adaccount'])
                res = {
                    'code': 0,
                    'message': '登陆成功'
                }
            else:
                res = {
                    'code': 1,
                    'message': '账号或密码错误'
                }
        except:
            res = {
                'code': 1,
                'message': '账号或密码错误'
            }
        self.write(res)
class logout(SHandler):
    def post(self):
        self.clear_all_cookies()
        self.write(dict(code = 0, message = '已退出'))
login_url = [
    (r"/api/login", login),
    (r"/api/logout", logout),
]
